﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using MyLocalChurch.Models;
using MyLocalChurch.Models.KidsChurch;

namespace MyLocalChurch
{
    public class MyContext : DbContext
    {
        public DbSet<ChurchService> ChurchServices { get; set; }

        public DbSet<Barcode> Barcodes { get; set; }
        public DbSet<Guardian> Guardians { get; set; }
        public DbSet<Kid> Kids { get; set; }
        public DbSet<Photo> Photos { get; set; }
        public DbSet<CheckInOut> CheckInOuts { get; set; }

        public MyContext() : base("MyLocalChurch")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Guardian>()
                        .HasMany(g => g.Kids)
                        .WithMany(k => k.Guardians)
                        .Map(m =>
                            {
                                m.MapLeftKey("Guardian_Id");
                                m.MapRightKey("Kid_Id");
                                m.ToTable("GuardianKidMapping");
                            });

            modelBuilder.Entity<Kid>()
                        .HasMany(k => k.CheckInOuts)
                        .WithOptional(c => c.Kid)
                        .WillCascadeOnDelete(true);

            modelBuilder.Entity<Photo>()
                        .Property(p => p.ImageData)
                        .HasColumnType("image");

            base.OnModelCreating(modelBuilder);
        }
    }
}